package com.its.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.its.mapper.BusClaimReportMapper;
import com.its.mapper.BusDispatchInfoMapper;
import com.its.mapper.BusInsurancePolicyMapper;
import com.its.pojo.BusClaimReport;
import com.its.pojo.BusDispatchInfo;
import com.its.pojo.BusSurveyInfo;
import com.its.mapper.BusSurveyInfoMapper;
import com.its.service.BusSurveyInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.its.util.HttpStatusEnum;
import com.its.util.Result;
import org.apache.ibatis.cache.decorators.TransactionalCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 朱厚强
 * @since 2024年01月04日
 */
@Service
public class BusSurveyInfoServiceImpl extends ServiceImpl<BusSurveyInfoMapper, BusSurveyInfo> implements BusSurveyInfoService {
    @Autowired
    private BusDispatchInfoMapper busDispatchInfoMapper;
    @Autowired
    private BusClaimReportMapper busClaimReportMapper;
    @Autowired
    private BusInsurancePolicyMapper busInsurancePolicyMapper;
    @Autowired
    private BusSurveyInfoMapper busSurveyInfoMapper;
    @Override
    public Result queryForDispatchListS() {
        LambdaQueryWrapper<BusDispatchInfo> wrapper = new LambdaQueryWrapper<>();
        // 查询查勘中的调度信息
        wrapper.eq(BusDispatchInfo::getSurveryState,1);
        // 未取消的调度信息
        wrapper.eq(BusDispatchInfo::getAssignStatus,1);

        List<BusDispatchInfo> list = busDispatchInfoMapper.selectList(wrapper);

        // 处理关联关系
        list.forEach(l ->{
            l.setClaimReport(busClaimReportMapper.selectById(l.getClaimReportId()));
        });
        if (list.size() > 0){
            return Result.ok().code(HttpStatusEnum.SUCCESS.getCode()).data(list).message("查询成功");
        }
        return Result.ok().code(HttpStatusEnum.ERROR.getCode()).message("查询失败");
    }

    @Override
    @Transactional
    public boolean surveySave(BusSurveyInfo busSurveyInfo) {
        try {
            // 添加信息
            int insert = busSurveyInfoMapper.insert(busSurveyInfo);
            // 更新报案表状态
            int i = busClaimReportMapper.updateById(new BusClaimReport().setClaimReportId(busSurveyInfo.getClaimReportId()).setFlowState(3));
            // 更新调度状态
            int i1 = busDispatchInfoMapper.updateById(new BusDispatchInfo().setId(busSurveyInfo.getDispatchId()).setSurveryState(2));
            return true;
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); // 手动回滚
            System.out.println("添加信息时出现异常"+e.getStackTrace());
            return false;
        }

    }

    @Override
    public Result queryClaimAndPolicy(Integer dispatchId) {
        try {
            BusDispatchInfo busDispatchInfo = busDispatchInfoMapper.selectById(dispatchId);
            // 查询报案信息
            BusClaimReport claimReport = busClaimReportMapper.selectById(busDispatchInfo.getClaimReportId());
            // 查询保单信息
            busDispatchInfo.setBusInsurancePolicy(busInsurancePolicyMapper.selectById(claimReport.getPolicyNumber()));
            // 赋值入调度对象
            busDispatchInfo.setClaimReport(claimReport);
            return Result.ok().message("查询成功").data(busDispatchInfo);
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return Result.error().message("查询失败");
        }
    }


    @Override
    public PageInfo<BusSurveyInfo> queryAll(Integer pageNum , Integer pageSize, String claimReportId) {
        // 开始分页
        PageHelper.startPage(pageNum,pageSize);
        LambdaQueryWrapper<BusSurveyInfo> wrapper = new LambdaQueryWrapper<>();
        System.out.println("---------------------------"+claimReportId);
        // 报案号
        if (claimReportId != null){
            wrapper.eq(BusSurveyInfo::getClaimReportId,claimReportId);
        }
        // 只查询存在的信息
        wrapper.eq(BusSurveyInfo::getExist,0);
        // 排序
        wrapper.orderByDesc(BusSurveyInfo::getSurveyTime);
        List<BusSurveyInfo> list = busSurveyInfoMapper.selectList(wrapper);
        // 处理关联关系
//        list.forEach(dList ->{
//            // 保存报案信息
//            dList.setBusClaimReport(busClaimReportMapper.selectById(dList.getClaimReportId()));
//            // 保存调度员信息
//            dList.setBusDispatchInfo(busDispatchInfoMapper.selectById(dList.getDispatchId()));
//        });
        return new PageInfo<>(list);
    }
}
